const number = require("@hapi/joi/lib/types/number");
const dayjs = require("dayjs");
function sendFun(res, myPersonResult, zhi) {
  if (myPersonResult && myPersonResult.affectedRows > 0) {
    res.send({
      code: 200,
      msg: "success",
      data: zhi + myPersonResult.affectedRows + "条成功",
    });
  } else {
    res.send({
      code: 0,
      msg: "fail",
      data: zhi + "失败",
    });
  }
}

exports.get = async function (req, res) {
  const sql = `SELECT * FROM public`;
  const myResult = await global.query(sql);
  if(myResult.length > 0){
    res.send({
      code: 200,
      msg: "success",
      data: myResult[0],
    });
  }else{
    res.send({
      code: 0,
      msg: "您还没有配置",
    });
  }

};
exports.updata = async function (req, res) {
  const { money_register, loading_json } = req.body;
  const sql = `SELECT * FROM public`;
  const myResultList = await global.query(sql);
  if (myResultList.length > 0) {
    const sql = `UPDATE public SET money_register = ? , loading_json = ? WHERE id = ?`;
    const myResult = await global.query(sql, [money_register, loading_json, myResultList[0].id]);
    sendFun(res, myResult, "更新")
  } else {
    // money_register,loading_json
    const sql = `INSERT INTO public(money_register, loading_json) VALUES(?,?)`;
    const myResult = await global.query(sql, [money_register, loading_json]);
    sendFun(res, myResult, "添加")
  }

};